package com.peaksport.framework.map;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiConsumer;

import com.peaksport.framework.exception.PKDevException;
import com.peaksport.framework.util.PKPublicUtils;

/**
 * 有序Map
 * 
 * @author jinbin_guo  2019年4月15日 下午5:29:08
 *
 * @param <V>
 */
@SuppressWarnings({ "serial" })
public class ListMap<V> implements Serializable {

    /** key 列表*/
    private List<String> lstKey = new ArrayList<>();

    private Map<String, V> map = new HashMap<>();

    public V get(int index) {
    	if (index >= lstKey.size()) 
    		throw PKDevException.throwException("ListMap.get(index),index[%d]超出ListNoCaseMap集合大小[%d]",index,lstKey.size()-1);
        String key = lstKey.get(index);
        return get(key);
    }
    public V get(String key) {
        return map.get(key);
    }

    public String getKey(int index) {
    	if (index >= lstKey.size()) 
    		throw PKDevException.throwException("ListMap.getKey(index),index[%d]超出ListNoCaseMap集合大小[%d]",index,lstKey.size()-1);
        return lstKey.get(index);
    }

    public void add(String key,V value) {
    	if (key == null) throw PKDevException.throwException("ListMap.add(key,value),key不能为空");
        if (map.get(key) == null) {
             lstKey.add(key);
        }
        map.put(key,value) ;
    }

    public void addALL(ListMap<V> otherListMap)  {
    	if (otherListMap == null || otherListMap.isEmpty()) return;
    	for (int i = 0,len = otherListMap.size();i < len;i++) {
    		String key = otherListMap.getKey(i);
            V value= otherListMap.get(key);
            add(key,value);
        }
    }

    public int size() {
        return lstKey.size();
    }

    public boolean isEmpty() {
        return lstKey.size() == 0;
    }

    public V remove(int index) {
    	if (index >= lstKey.size()) 
    		throw PKDevException.throwException("ListMap.remove(index),index[%d]超出ListNoCaseMap集合大小[%d]",index,lstKey.size()-1);
        String key = lstKey.get(index);
        return remove(key);
    }

    public V remove(String key) {
        lstKey.remove(key);
        return map.remove(key);
    }

    public void clear() {
        lstKey.clear();;
        map.clear();
    }

    public Map<String,V> getMap() {
        return map;
    }
    
    public Collection<V> values() {
    	return map.values();
    }
    
    public List<String> getKeys() {
    	return lstKey;
    }
    
    /**
     * 按key值从小到大重新排序
     */
    public void sortKey() {
    	sortKey(false);
    }
    /**
     * 按key值重新排序
     * @param isReverseOrder 是否反向排序(从大到小)
     */
    public void sortKey(boolean isReverseOrder) {
    	if (isReverseOrder) Collections.sort(lstKey,Collections.reverseOrder());
    	else Collections.sort(lstKey);
    }


    @SuppressWarnings("unchecked")
	public ListMap<V> clone() {
        return (ListMap<V>) PKPublicUtils.clone(this);
    }

    void forEach(BiConsumer<? super String, ? super V> action) {
        Objects.requireNonNull(action);
        for (int i = 0, len = lstKey.size(); i < len; i++) {
        	String key = lstKey.get(i);
        	V v = map.get(key);
        	action.accept(key, v);
        }
    }
    
   
}

